#ifndef _RCU_MSG_H_
#define _RCU_MSG_H_

#include "struct.h"
extern UDP_DAEMON_ udp;

#define IPC_OFFSET_OF_CRC_START	4 	//int8u start[4]
#define IPC_KTI_HEADER_LENGTH	sizeof(IPC_Header_T)
#define IPC_HEADER_LENGTH		IPC_KTI_HEADER_LENGTH
#define IPC_POS_OF_LEN		(IPC_KTI_HEADER_LENGTH-2) 	//2 indicates size of len

#define IPC_KTI_DATA_LENGTH	sizeof(IPC_Data_T)
#define IPC_SIZEOF_KTI_TAIL	4 //sizeof(int16u crc) + sizeof(int8u endflag[2]

//KTI Command Define
#define KTC_STATUS_REPORT	0x22
#define KTC_ALARM_REPORT	0x23
#define KTC_CONTRL_REQ		0x24

#define KTC_LOCAL_REQ		0x2A
#define KTC_LOCAL_RSP		0x2B
#define KTC_LOCAL_CMD3		0x2C

#define KTC_IP_CHANGE_REQ	0x13
#define KTC_IP_CHANGE_RSP	0x14

#define KTC_RESET_REQ		0x20
#define KTC_RESET_RSP		0x21

#define KTC_STATUS_REQ		0x30

#define KTC_DL_CMD_REQ		0x50
#define KTC_DL_CMD_RSP		0x51

#define KTC_DL_DATA			0x52
#define KTC_DL_DATA_RSP		0x53

#define KTC_DL_COMPLETE_REQ		0x54
#define KTC_DL_COMPLETE_RSP		0x55

#define KTC_MAC_ADDR_REQ		0x19
#define KTC_MAC_ADDR_RSP		0x1A

#define KTC_HEARTBEAT_REQ		0x1D
#define KTC_HEARTBEAT_RSP		0x1E

//KTI Header Define

#define M_KTI_NICK_NAME 0x1F
#define M_KTI_NMS_VERSION 10
#define M_KTI_RCU_VERSION M_RCU_VERSION

//#define GetMasterID() "ID:123456$"
//#define GetMasterID() "RCUT10001T"
#define GetMasterID() sicf.v[7]
#define GetEquipNum() "-ABCDEF-"

//KTI Report Define
//extern int iPeriodUdpReport ;
#define GetReportPeriod()   CONFIG.iPeriodUdpReport //10 //unit minute
//#define GetSerialNumber() 	"SN:12345678901$"
#define GetSerialNumber() 	sicf.v[4]
#define GetTimeValid()		0x11
#define GetTimeMode()		0x22
#define GetReportFlag() 	0x33
#define GetLeapSeconds()	16
#define GetRunningTime()	30000
#define GetHoldoverTime()	20

#define GetRcuStatus()	2

#define GetDacValue()	32500

#define GetTimeOffsetNanoSeconds() 150
#define GetFreqAccuracy1E_12()	   111 //1e-10

#define GetGpsVisible()		10
#define GetGpsTracking()	8
#define GetGpsDop()		14 // should be divide by 10
#define GetGpsCnMean()	45
#define GetGpsCnMax()	51
#define GetGpsCnMin()	42
#define GetGpsAntStatus() 2
#define GetTemperature()	25

#define GetTotalCrc()	0x5577;


char *GetCmdTitle(int8 cmd);
///// msg
//int net_send(char *p, unsigned int length,int debug_send);
//int net_send(UDP_DAEMON_* ud, unsigned int length, int debug_send);
int msg_ntoh_data(int8 *p_buf,int8u cmd);
int msg_hton_data(int8 *p_buf,int8u cmd);

//// msg rx
int msg_unpack_header(UDP_DAEMON_* ud);

//// msg tx
int msg_pack_header(IPC_Data_T *p,int seq);
int msg_pack_data_report(UDP_DAEMON_* ud,int seq);

void make_rcu_alarm(KTI_SubData_Alm_T *p);
int msg_set_data_alarm(KTI_SubData_Alarm_T* p);
int msg_set_data_report(KTI_SubData_Report_T* p);
int msg_set_control_req(KTI_SubData_CtrlReq_T* p);
int msg_set_ip_req(KTI_SubData_IpReq_T* p);

int msg_set_download_req(KTI_SubData_DL_CmdReq_T* p);
int msg_set_download_rsp(KTI_SubData_DL_CmdRsp_T* p);

int msg_set_download_complete_req(KTI_SubData_DL_CompleteReq_T* p);
int msg_set_download_complete_rsp(KTI_SubData_DL_CompleteRsp_T* p,int16u crc, int8 rlt);
int msg_set_mac_rsp(KTI_SubData_MacRsp_T* p);


//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////

int msg_pack_status_req(UDP_DAEMON_* ud,int seq);
int msg_pack_data_report(UDP_DAEMON_* ud,int seq);

int msg_pack_data_alarm(UDP_DAEMON_* ud,int seq);
int msg_pack_control_req(UDP_DAEMON_* ud,int seq);

int msg_pack_reset_req(UDP_DAEMON_* ud,int seq);
int msg_pack_reset_rsp(UDP_DAEMON_* ud,int seq);

int msg_pack_mac_req(UDP_DAEMON_* ud,int seq);
int msg_pack_mac_rsp(UDP_DAEMON_* ud,int seq);

int msg_pack_download_req(UDP_DAEMON_* ud,int seq);
//int msg_pack_download_rsp(UDP_DAEMON_* ud,int seq);
int msg_pack_download_rsp(UDP_DAEMON_* ud,int seq,KTI_SubData_DL_CmdRsp_T *ptr);

int msg_pack_download_data(UDP_DAEMON_* ud,int seq,char *buf,int size);
//int msg_pack_download_data(UDP_DAEMON_* ud,int seq);
//int msg_pack_download_data_rsp(UDP_DAEMON_* ud,int seq);
int msg_pack_download_data_rsp(UDP_DAEMON_* ud,int seq,KTI_SubData_DL_DataRsp_T *ptr);

int msg_pack_download_complete_req(UDP_DAEMON_* ud,int seq);
int msg_pack_download_complete_rsp(UDP_DAEMON_* ud,int seq,int16u crc,int8 rlt);

int msg_pack_CmdReq(UDP_DAEMON_* ud,int seq,char *buf,int size); //LOCAL REQ
int msg_pack_CmdRsp(UDP_DAEMON_* ud,int seq,char *buf,int size); //LOCAL RSP

int msg_pack_ip_req(UDP_DAEMON_* ud,int seq);
int msg_pack_ip_rsp(UDP_DAEMON_* ud,int seq);

int msg_respond(char *p,int cmd, int len);

int GetDownloadTotalFrame();
int GetDownloadCurrentFrame();
char *GetDownloadCurrentPointer();
int GetDownloadCurrentSize();

#define init_DL(x) do{ x.iElapsed =0; x.iSubElapsed; x.iTry; }while(0)

void my_htons(char *p);
void my_htonl(char *p);
void my_ntohs(char *p);
void my_ntohl(char *p);

void SetUdpMsg();

int msg_pack_data_report2nd(UDP_DAEMON_* ud,int seq,int flag);
int msg_pack_data_heartbeat_rsp(UDP_DAEMON_* ud,int seq);
int msg_pack_data_heartbeat();

extern void udp_local_cmd_send_size(char* p,int len);

#endif
